//
//  HZCartCellModel.m
//  app
//
//  Created by 刘华舟 on 15/5/4.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZCartCellModel.h"

#import "HZMallSqlDataManager.h"
#import "JSONKit.h"
#import "NSDictionary+SafetyGet.h"


@implementation HZCartCellModel

+ (instancetype)modelWithDictionary:(NSDictionary *)data type:(HZCartTableViewCellType)type{
    return [[HZCartCellModel alloc] initWithDictionary:data type:(HZCartTableViewCellType)type];
}
- (instancetype)initWithDictionary:(NSDictionary *)data type:(HZCartTableViewCellType)type{
    if (self = [super init]) {
        _cellType = type;
        _actID = [data stringForKey:APIKEY_mall_act_id nilValue:@"1"];
        _shopID = [data stringForKey:APIKEY_mall_shop_id nilValue:@"0"];
        _shopName = [data stringForKey:APIKEY_mall_shop_name nilValue:@""];
        _goodsID = [data stringForKey:APIKEY_mall_product_id nilValue:@"0"];
        _goodsTitle = [data stringForKey:APIKEY_mall_product_title nilValue:@""];
        _picURL = [data stringForKey:APIKEY_mall_pic nilValue:@""];
        _picWidth = [data stringForKey:APIKEY_mall_width nilValue:@"200"];
        _picHeight = [data stringForKey:APIKEY_mall_height nilValue:@"200"];
        _goodsPriceNow = [data stringForKey:APIKEY_mall_sale_price nilValue:@"0.00"];
        _goodsPriceNow = [NSString stringWithFormat:@"%.2f",_goodsPriceNow.floatValue];
        _goodsPriceOriginal = [data stringForKey:APIKEY_mall_tag_price nilValue:@"0.00"];
        _goodsPriceOriginal = [NSString stringWithFormat:@"%.2f",_goodsPriceOriginal.floatValue];
        _goodsCountNum = [data stringForKey:APIKEY_mall_buy_num nilValue:@"1"];
        _reserveNum = [data stringForKey:APIKEY_mall_reserveNum nilValue:@"0"];
        
        _productType = [data stringForKey:APIKEY_mall_product_type nilValue:@"normal"]; //默认是正常商品
        
        _srvStartTime = [data stringForKey:APIKEY_mall_starttime nilValue:@"0"]; //默认无起售时间
        _srvEndTime = [data stringForKey:APIKEY_mall_endtime nilValue:@"0"];    //默认无结束时间
        
        _cellHeight = floor(UF(208));
        
        _giftModel = [SNGiftModel modelWithDict:[data dictForKey:APIKEY_mall_gift]];
        
        
        
  
        _promotionArray = [data arrayForKey:APIKEY_mall_promotion];
        
        
        if (_giftModel.gift || _promotionArray.count) {
            NSInteger col = 0;
            if (_giftModel) {
                col = 1;
            }
//            if (_promotionArray.count) {
//                col += 1;
//            }
            _cellHeight += [UBO_SCREEN uh:kSizeMallViewMargin oh:kGiftViewHeight*(col)];
        }

    }
    
    _patterns = [NSJSONSerialization JSONObjectWithData:[[data stringForKey:APIKEY_mall_patterns nilValue:@"[]"] dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];

    
    _canBuy = YES;
    
    return self;
}

- (void)setSelected:(BOOL)selected{
    _selected = selected;
    
    HZMallSqlDataManager *sqlDataManager = [HZMallSqlDataManager defaultManager];
    [sqlDataManager updateProductChoseStatuWithProductID:self.goodsID selected:selected];
}

@end
